<?php

class Controller extends Yaf_Controller_Abstract {

    /**
     * showMessage
     *
     * @return void
     */
    public function showMessage($msg, $errCode = 0, $data = array(), $extra = array()) {

        $responseType = strtolower($this->_request->get('resType', 'json'));

        $locationTime = 2000;
        if (isset($extra['locationTime'])) {
            $locationTime = intval($extra['locationTime']);
        }
        $result = array(
            'errCode' => $errCode,
            'srvCode' => $extra['srvCode'],
            'message' => $msg,
            'data' => $data,
            'showLogin' => $extra['showLogin'],
            'jumpURL' => $extra['jumpURL'],
            'locationTime' => $locationTime,
        );

        switch ($responseType) {
        case 'php':
            $this->_response->setBody(serialize($result));
            break;
        case 'jsonp':
            $this->_response->setHeader('Content-Type', 'text/javascript; charset=utf-8');
            if ($extra['tpl']) {
                $this->initView();
                $this->_view->display($extra['tpl']);
            } else {
                $jsCallBackFunc = $this->_request->get('callback', 'callback');
                if ($extra['callback']) {
                    $jsCallBackFunc = $extra['callback'];
                }
                $jsonData = json_encode($result);
                $this->_response->setBody($jsCallBackFunc . '(' . $jsonData . ')');
            }
            break;
        default:
            $this->_response->setHeader('Content-Type', 'application/json; charset=utf-8');
            $this->_response->setBody(json_encode($result));
        }

        $this->_response->response();
        exit;
    }

    public function errorMessage($msg, $code, $srvCode = 0, $extra = array(), $data = array()) {

        $extra['srvCode'] = $srvCode;
        $msg .= ' [errcode:'.$code.':'.$srvCode.']';
        $this->showMessage($msg, $code, $data, $extra);

        return true;
    }

    public function successMessage($msg, $data = array(), $extra = array()) {

        $this->showMessage($msg, 0, $data, $extra);

        return true;
    }
}
